package com.stylefeng.guns.modular.purchase.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.tips.ErrorTip;
import com.stylefeng.guns.core.datascope.DataScope;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.core.util.IdGenerator;
import com.stylefeng.guns.modular.dto.ReturnGoodsAddRequest;
import com.stylefeng.guns.modular.dto.ReturnListQueryRequest;
import com.stylefeng.guns.modular.purchase.service.IGoodsReturnListService;
import com.stylefeng.guns.modular.system.model.GoodsReturn;
import com.stylefeng.guns.modular.system.dao.GoodsReturnMapper;
import com.stylefeng.guns.modular.purchase.service.IGoodsReturnService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.modular.system.model.GoodsReturnList;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author LiuXin
 * @since 2018-09-12
 */
@Service
public class GoodsReturnServiceImpl extends ServiceImpl<GoodsReturnMapper, GoodsReturn> implements IGoodsReturnService {

    @Autowired
    private IGoodsReturnListService goodsReturnListService;

    @Override
    public Page<GoodsReturn> selectReturnPageAndDataScope(Page<GoodsReturn> page, ReturnListQueryRequest request) {

        EntityWrapper<GoodsReturn> entityWrapper = new EntityWrapper<>();
        //订单号
        if (StringUtils.isNotBlank(request.getReturnOrderNumber())) {
            entityWrapper.eq("return_order_number", request.getReturnOrderNumber());
        }

        //是否支付
        if (StringUtils.isNotBlank(request.getPayState())) {
            entityWrapper.eq("pay_state", Integer.valueOf(request.getPayState()));
        }

        //开始日期 大于等于
        if (StringUtils.isNotBlank(request.getbDate())) {
            entityWrapper.ge("create_date", DateUtil.parse(request.getbDate(), "yyyy-MM-dd"));
        }

        //结束日期 小于等于
        if (StringUtils.isNotBlank(request.geteDate())) {
            entityWrapper.le("create_date", DateUtil.parse(request.geteDate(), "yyyy-MM-dd"));
        }

        //根据备注信息查询
        if (StringUtils.isNotBlank(request.getRemarks())) {
            entityWrapper.like("remarks", request.getRemarks());
        }

        if(ShiroKit.isAdmin()){
            Page<GoodsReturn> result = this.selectPage(page, entityWrapper);
            return result;
        }else {
            DataScope dataScope = new DataScope(ShiroKit.getDeptDataScope());
            List<GoodsReturn> goodsReturns = this.baseMapper.selectReturnPageWithDataScope(page,dataScope,request);
            page.setRecords(goodsReturns);
            return page;
        }
    }

    @Override
    public boolean insertListAndGoods(ReturnGoodsAddRequest request) {
        GoodsReturn goodsReturn = new GoodsReturn();
        BeanUtils.copyProperties(request, goodsReturn);
        boolean success = this.insert(goodsReturn);
        Long goodsReturnId = goodsReturn.getId();
        request.getReturnGoodsList().forEach(goodsReturnList -> goodsReturnList.setReturnGoodsListId(goodsReturnId));
        boolean insertGoodsReturnListSuccess = goodsReturnListService.insertBatch(request.getReturnGoodsList());
        return success && insertGoodsReturnListSuccess;
    }

    @Override
    public boolean deleteOrderAndGoods(Long goodsReturnListId) {
        EntityWrapper<GoodsReturnList> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("return_goods_list_id", goodsReturnListId);
        boolean success = goodsReturnListService.delete(entityWrapper);
        boolean deleteSuccess = this.deleteById(goodsReturnListId);
        return success && deleteSuccess;
    }
}
